import { prisma } from '@/lib/prisma/client'
import { NextResponse } from 'next/server'

export async function POST(request: Request) {
  try {
    const { groupId, sourceIndex, targetIndex } = await request.json()

    // 获取所有分组
    const groups = await prisma.sceneGroup.findMany({
      orderBy: {
        createdAt: 'asc'
      }
    })

    // 更新顺序
    const group = groups[sourceIndex]
    groups.splice(sourceIndex, 1)
    groups.splice(targetIndex, 0, group)

    // 批量更新顺序
    await prisma.$transaction(
      groups.map((group, index) =>
        prisma.sceneGroup.update({
          where: { id: group.id },
          data: { order: index }
        })
      )
    )

    return NextResponse.json({ message: '重新排序成功' })
  } catch (error) {
    return NextResponse.json(
      { error: '重新排序失败' },
      { status: 500 }
    )
  }
} 